package cn.ekuma.epos.productsfinishing;

import java.util.Properties;

import com.openbravo.bean.ProductScore;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;


import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import com.openbravo.pos.base.AppLocal;

import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JRadioButton;


public class ProductScoreEditor extends AbstractChildDAOJEditor<ProductScore,String> {
	private JTextField textField;
	private JRadioButton radioButton;
	private JRadioButton radioButton_1;

	public ProductScoreEditor(DirtyManager dirty) {
		super(dirty);
		setPreferredSize(new Dimension(400, 88));
		initComponents();
		textField.getDocument().addDocumentListener(dirty);
	}
	private void initComponents() {
		setLayout(null);
		
		JLabel label = new JLabel();
		label.setBounds(48, 10, 93, 29);
		label.setText(AppLocal.getIntString("label.score"));
		add(label);
		
		textField = new JTextField();
		textField.setBounds(151, 11, 158, 27);
		add(textField);
		
		JLabel label_1 = new JLabel(AppLocal.getIntString("label.ProductScore.type"));
		label_1.setBounds(48, 49, 93, 15);
		add(label_1);
		
		 radioButton = new JRadioButton(AppLocal.getIntString("label.ProductScore.type.store"));
		radioButton.setBounds(143, 45, 113, 23);
		add(radioButton);
		
		 radioButton_1 = new JRadioButton(AppLocal.getIntString("label.ProductScore.type.online"));
		radioButton_1.setBounds(258, 45, 121, 23);
		add(radioButton_1);
		
		javax.swing.ButtonGroup group = new javax.swing.ButtonGroup();
		group.add(radioButton);
		group.add(radioButton_1);
	}

	@Override
	public void refresh() {
		
	}

	@Override
	public String getTabName() {
		return AppLocal.getIntString("label.score");
	}

	@Override
	public ProductScore createNew(Properties prop) {
		return new ProductScore(parentNode.getParentKey().getKey());
	}

	@Override
	public void rebuild(ProductScore obj) throws BasicException {
		obj.setScore((Double) Formats.DOUBLE.parseValue(textField.getText()));
		if(radioButton_1.isSelected())
		   obj.setType(ProductScore.TYPE_Online);
		else
			obj.setType(ProductScore.TYPE_Store);
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		textField.setText(Formats.DOUBLE.formatValue(obj.getScore()));
		if(state==ProductScore.TYPE_Online)
			radioButton_1.setSelected(true);
		else
			radioButton.setSelected(true);
		textField.setEditable(editAble);
		radioButton_1.setEnabled(editAble);
		radioButton.setEnabled(editAble);
	}

	@Override
	public Class getEditClass() {
		return ProductScore.class;
	}
}
